package com.situ.mvc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.mvc.dao.IStudentDao;
import com.situ.mvc.util.JDBCUtil;
import com.situ.mvc.util.ModelConvertUtil;
import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentSearchCondition;


/**
 * 用jdbc来实现DAO
 * 
 * 
 *
 */
public class StudentDaoImpl implements IStudentDao {


	@Override
	public int insert(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(name,age,gender,banji_id) values(?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getAge());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getBanjiId());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public List<Student> list() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name,age,gender FROM student";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public int deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override//根据查找
	public Student findById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Student student = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT s.id AS s_id ,s.name AS s_name,s.age AS s_age,s.gender AS s_gender,b.name AS b_name FROM student AS s INNER JOIN banji AS  b ON s.banji_id = b.id WHERE s.id = ?";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer stuId = resultSet.getInt("s_id");//重复
				String stuName = resultSet.getString("s_name");
				Integer age = resultSet.getInt("s_age");
				String gender = resultSet.getString("s_gender");
				String banjiName = resultSet.getString("b_name");
				student = new Student(stuId, stuName, age, gender, banjiName);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return student;
	}

	@Override//更新
	public int update(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name=?,age=?,gender=?,banji_id=? where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, student.getName());
			preparedStatement.setInt(2, student.getAge());
			preparedStatement.setString(3, student.getGender());
			preparedStatement.setInt(4, student.getBanjiId());
			preparedStatement.setInt(5, student.getId());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	//原pageList页面
	//@Override
	public List<Student> pageList1(int offset, int pageSize) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name,age,gender FROM student limit ?,?";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, offset);
			preparedStatement.setInt(2, pageSize);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return list;
	
	}
	
	//带班级 的信息的pageList
	@Override
	public List<Map<String, Object>> pageList(int offset, int pageSize) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT s.id AS s_id ,s.name AS s_name,s.age AS s_age,s.gender AS s_gender,b.name AS b_name FROM student AS s INNER JOIN banji AS  b ON s.banji_id = b.id limit ?,?";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, offset);
			preparedStatement.setInt(2, pageSize);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			list = ModelConvertUtil.converToList(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return list;
	
	}
	

	@Override
	public int getTotalCount() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT count(id) FROM student";
			preparedStatement = connection.prepareStatement(sql);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				count =  resultSet.getInt(1);//第一列
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return count;
	}

	/**
	 * 批量删除
	 */
	@Override
	public int deleteAll(String[] selectIds) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			for (String id : selectIds) {
				preparedStatement.setInt(1, Integer.parseInt(id));
				preparedStatement.addBatch();
			}
			int[] counts = preparedStatement.executeBatch();
			count = counts.length;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				JDBCUtil.close(connection, preparedStatement, null);
			}
			return count;
	}

	@Override
	public List<Student> findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * ajax添加学生姓名不重复
	 */
	@Override
	public int selectCountByName(String name) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT count(id) from	student where name=?";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, name);
			
			resultSet = preparedStatement.executeQuery();
				if (resultSet.next()) {
					count = resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return count;
	}

	//搜索拿到总记录数
	@Override
	public int getTotalCount(StudentSearchCondition searchCondition) {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT COUNT(*) FROM student AS s INNER JOIN banji AS b ON s.banji_id = b.id "
					+ " where 1=1 ";
					//什么都不写展示出全部的	
					//+ "where s.name like ? and s.age=? and s.gender=? ";

			List conditions = new ArrayList<>();
			if (searchCondition.getName() != null 
					&& !"".equals(searchCondition.getName())) {
				sql += " and s.name like ?";
				conditions.add("%" + searchCondition.getName() + "%");
			}
			if (searchCondition.getAge() != null) {
				sql += " and s.age = ?";
				conditions.add(searchCondition.getAge());
			}
			if (searchCondition.getGender() != null 
					&& !"".equals(searchCondition.getGender())) {
				sql += " and s.gender = ?";
				conditions.add(searchCondition.getGender());
			}
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 0; i < conditions.size(); i++) {
				preparedStatement.setObject(i + 1, conditions.get(i));
			}
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				count =  resultSet.getInt(1);//第一列
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return count;
	}
	
	//搜索拿到当前页的数据
	@Override
	public List<Map<String, Object>> pageList(StudentSearchCondition searchCondition) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT s.id AS s_id,s.name AS s_name,s.age AS s_age,s.gender AS s_gender,b.name AS b_name FROM student AS s INNER JOIN banji AS b ON s.banji_id = b.id"
					+ " where 1=1 ";
					//+ "where s.name like ? and s.age=? and s.gender=? limit ?,? ";
			// 预编译sql
			List conditions = new ArrayList<>();
			if (searchCondition.getName() != null 
					&& !"".equals(searchCondition.getName())) {
				sql += " and s.name like ?";
				conditions.add("%" + searchCondition.getName() + "%");
			}
			if (searchCondition.getAge() != null) {
				sql += " and s.age = ?";
				conditions.add(searchCondition.getAge());
			}
			if (searchCondition.getGender() != null 
					&& !"".equals(searchCondition.getGender())) {
				sql += " and s.gender = ?";
				conditions.add(searchCondition.getGender());
			}
			sql += " limit ?,? ";
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 0; i < conditions.size(); i++) {
				preparedStatement.setObject(i + 1, conditions.get(i));
			}
			int offset = (searchCondition.getPageNo() - 1) * searchCondition.getPageSize();
			preparedStatement.setInt(conditions.size() + 1, offset);
			preparedStatement.setInt(conditions.size() + 2, searchCondition.getPageSize());
			
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			list = ModelConvertUtil.converToList(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return list;
	}

}
